<?php
namespace app\common\process;

class Info extends Automatic {
    protected $pk = 'id';
    protected $row = 'row';
    protected $upk = 'id';
    protected $uid = 'uid';
    protected $with = [];
    protected $privateListScene = 'private';
    protected $lookScene = 'look';
    protected $infoScene = 'info';
    protected $delScene = 'del';
    protected $delsScene = 'dels';
    protected $statusScene = 'status';
    protected $previewScene = 'preview';
    protected $showScene = 'show';
    protected $saveScene = 'save';
    protected $append = ['is_show'];
    protected $limit = 'limit';

    //获取内容列表
    public function list($fun = null) {
        $limit = input('param.' . $this->limit);
        $model = $this->model;
        if(is_callable($fun)) {
            $fun($model);
        }
        $model->privacy()->show_list();
        if($limit) {
            $list = $model->paginate($limit);
        } else {
            $list = $model->select();
        }
        $this->assign('list', $list);
        return $this->fetch();
    }

    //获取私密列表
    public function private_list($user, $fun = null) {
        $limit = input('param.' . $this->limit);
        $model = $this->model;
        $validate = $this->validate;
        $post = [];
        if(isset($user[$this->upk])) {
            $post[$this->uid] = $user[$this->upk];
        }
        if (!$validate->scene($this->privateListScene)->check($post)) {
            $this->error($validate->getError());
        }
        $aliasName = $model->aliasName;
        if($aliasName) {
            $aliasName = $aliasName . '.';
        }
        $model->where([$aliasName . $this->uid => $user[$this->upk]]);
        if(is_callable($fun)) {
            $fun($model);
        }
        if($limit) {
            $list = $model->paginate($limit);
        } else {
            $list = $model->select();
        }
        $this->assign('list', $list);
        return $this->fetch();
    }

    //查看，只能查看公开内容，不需要验证
    public function look() {
        $model = $this->model;
        $validate = $this->validate;
        $with = $this->with;

        $id = input('param.' . $this->pk);
        $post = [];
        $post['id'] = $id;
        if (!$validate->scene($this->lookScene)->check($post)) {
            $this->error($validate->getError());
        }
        $row = $model->find($id, true);
        if($row) {
            $row = $row->privacy();
            $row = $row->toArray();
        }
        $this->assign($this->pk, $id);
        $this->assign($this->row, $row);
        return $this->fetch();
    }

    //查看，只能查看公开信息
    public function look_all() {
        $model = $this->model;
        $validate = $this->validate;
        $with = $this->with;

        $id = input('param.' . $this->pk);
        $post = [];
        $post['id'] = $id;
        if (!$validate->scene($this->lookScene)->check($post)) {
            $this->error($validate->getError());
        }
        $row = $model->with($with)->find($id, true);
        if($row) {
            $row = $row->append($this->append);
            $row = $row->privacy($with);
            $row = $row->toArray();
        }
        $this->assign($this->pk, $id);
        $this->assign($this->row, $row);
        return $this->fetch();
    }


    //只查看直接数据,可用于编辑
    public function info($user) {
        $model = $this->model;
        $validate = $this->validate;

        $id = input('param.' . $this->pk);
        $post = [];
        $post[$this->pk] = $id;
        $post[$this->uid] = $user[$this->upk];

        if (!$validate->scene($this->infoScene)->check($post)) {
            $this->error($validate->getError());
        }

        $row = null;
        if($id) {
            $row = $model->find($post[$this->pk]);
        }
        $this->assign($this->pk, $id);
        $this->assign($this->row, $row);
        return $this->fetch();
    }

    //私有查看全部信息，需要验证
    public function preview_look($user, $fun = null) {
        $validate = $this->validate;

        $id = input('param.' . $this->pk);
        $post = [];
        $post['id'] = $id;
        $post[$this->uid] = $user[$this->upk];
        if (!$validate->scene($this->previewScene)->check($post)) {
            $this->error($validate->getError());
        }
        return $this->preview_look_ipl($id, $fun);
    }

    //私有查看全部信息实际执行类
    public function preview_look_ipl($id, $fun = null) {
        $model = $this->model;
        $model->with($this->with);
        if(is_callable($fun)) {
            $fun($model);
        }
        $row = $model->find($id);
        if($row) {
            $row = $row->append($this->append);
        }
        $this->assign($this->pk, $id);
        $this->assign($this->row, $row);
        return $this->fetch();
    }



    //用户获取自己的基础信息
    public function user_info($user) {
        $model = $this->model;
        $row = $model->where([$this->uid => $user[$this->upk]])->find();
        $this->assign($this->row, $row);
        return $this->fetch();
    }

    //用户查看自己的全部信息，无需传数据ID
    public function user_look($user) {
        $model = $this->model;
        $row = $model->with($this->with)->where([$this->uid => $user[$this->upk]])->find();
        if($row) {
            $row = $row->append($this->append);
        }
        $this->assign($this->row, $row);
        return $this->fetch();
    }


    //获取保存数据
    public function get_save_data() {
        $post = input('post.');
        unset($post['status']); //禁止前台传递状态
        unset($post['examine']);//禁止前台传递审核状态
        return $post;
    }

    public function before_save($model, $post) {

    }

    public function after_save($model, $post, $sid) {

    }

    //保存
    public function save($user) {
        $model = $this->model;
        $validate = $this->validate;

        $post = $this->get_save_data();
        $post[$this->uid] = $user[$this->upk];
        $pk = $this->pk;

        if (!$validate->scene($this->saveScene)->check($post)) {
            $this->error($validate->getError());
        }

        try {
            $model->startTrans();
            $this->before_save($model, $post);
            if(isset($post[$pk]) && $post[$pk]) {
                $model->allowField(true)->save($post, [$pk => $post[$pk]]);
            } else {
                $model->allowField(true)->save($post);
                $post[$pk] = $model->id;
            }
            $this->after_save($model, $post, $model->id);
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }


        $this->assign($pk, $model->$pk);
        return $this->fetch();
    }

    //删除关联
    public function delAll($user) {
        $model = $this->model;
        $validate = $this->validate;
        
        $id = input('param.' . $this->pk);
        $post = [];
        $post[$this->pk] = $id;
        $post[$this->uid] = $user[$this->upk];

        if (!$validate->scene($this->delScene)->check($post)) {
            $this->error($validate->getError());
        }
        if(method_exists($model, 'delAll')) {
            return $model->delAll($id);
        } else {
            return $model->where([$this->pk => $id])->delete();
        }   
    }

    //批量删除
    public function delAll4Ids($user) {
        $model = $this->model;
        $validate = $this->validate;

        $ids = input('param.' . $this->pk . 's');
        $post = [];
        $post[$this->pk . 's'] = $ids;
        $post[$this->uid] = $user[$this->upk];

        if (!$validate->scene($this->delsScene)->check($post)) {
            $this->error($validate->getError());
        }

        if(method_exists($model, 'delAll')) {
            return $model->delAlls($id);
        } else {
            return $model->whereIn($this->pk, $ids)->delete();
        }  

    }

    //清空自己的数据
    public function clear($user) {
        return $this->model->where([$this->uid => $user[$this->upk]])->delete();
    }

    //修改状态值
    public function setStatus($user) {
        $model = $this->model;
        $validate = $this->validate;

        $post = input('post.');
        $post[$this->uid] = $user[$this->upk];
        $pk = $this->pk;

        if (!$validate->scene($this->statusScene)->check($post)) {
            $this->error($validate->getError());
        }
        $model->allowField(['status'])->save($post, [$pk => $post[$pk]]);
        $this->assign($pk, $model->$pk);
        return $this->fetch();
    }

    //显示内容
    public function show($user) {
        $model = $this->model;
        $validate = $this->validate;

        $id = input('param.' . $this->pk);
        $post = [];
        $post[$this->pk] = $id;
        $post[$this->uid] = $user[$this->upk];

        if(!$validate->scene($this->statusScene)->check($post)) {
            $this->error($validate->getError());
        }
        if($validate->isScene($this->showScene)) {
            if(!$validate->scene($this->showScene)->check($post)) {
                $this->error($validate->getError());
            }
        }
        $model->show($post[$this->pk]);
        $this->assign('__msg', '保存成功');
        return $this->fetch();
    }

    //隐藏内容
    public function hide($user) {
        $model = $this->model;
        $validate = $this->validate;

        $id = input('param.' . $this->pk);
        $post = [];
        $post[$this->pk] = $id;
        $post[$this->uid] = $user[$this->upk];

        if(!$validate->scene($this->statusScene)->check($post)) {
            $this->error($validate->getError());
        }
        $model->hide($post[$this->pk]);
        $this->assign('__msg', '保存成功');
        return $this->fetch();
    }
}